package org.oimwrapper.testers;

import Thor.API.Operations.*;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;

import Thor.API.tcResultSet;
import com.thortech.util.logging.Logger;
import com.thortech.xl.dataaccess.tcVariantTypes;
import org.junit.Test;
import org.oimwrapper.connectors.OIMAdapterBase;
import org.oimwrapper.connectors.OIMRemoteClientBase;
import org.oimwrapper.connectors.internal.OIMObjectFormUtilities;
import org.oimwrapper.connectors.internal.OIMUtilityGetter;
import org.oimwrapper.testers.ClientBaseTester;
import org.oimwrapper.testers.ClientUserTester;

public class OracleSRTest2 extends ClientBaseTester {

    static SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    static boolean debugOn;
    private static final Logger logger = Logger.getLogger(OracleSRTest2.class.getName());
    private static OIMAdapterBase apiBase;
    private OIMRemoteClientBase tester;
    private tcFormInstanceOperationsIntf formInstOps;
    private tcFormDefinitionOperationsIntf formDefOps;
    private tcObjectOperationsIntf objectOp;
    private OIMObjectFormUtilities objFormUtils;
    private tcProvisioningOperationsIntf provOps = null;
    private tcRequestOperationsIntf requestOps;

    @Test
    public void mainTest() throws Exception {

        OIMRemoteClientBase tester = null;
        try {
            tester = this.connect();
            apiBase = tester.getUtils();

            formInstOps = (tcFormInstanceOperationsIntf) apiBase.getUtility(Thor.API.Operations.tcFormInstanceOperationsIntf.class);
            formDefOps = (tcFormDefinitionOperationsIntf) apiBase.getUtility(Thor.API.Operations.tcFormDefinitionOperationsIntf.class);
            objectOp = (tcObjectOperationsIntf) apiBase.getUtility(Thor.API.Operations.tcObjectOperationsIntf.class);
            objFormUtils = OIMUtilityGetter.getObjectFormUtilities(logger, objectOp, formInstOps, formDefOps);
            provOps = (tcProvisioningOperationsIntf) apiBase.getUtility(Thor.API.Operations.tcProvisioningOperationsIntf.class);
            requestOps = (tcRequestOperationsIntf) tester.getUtils().getUtility(Thor.API.Operations.tcRequestOperationsIntf.class);
        } catch (Exception ex) {
            logger.error("Exception", ex);
            return;
        }

        //provnonpaged();
        provpaged();
        //provsgpaged();
        //approvals();
        return;
    }

    public void provnonpaged() {

        logger.info("*********** NONPAGING API ***********");
        try {

            String[] statusArray = new String[2];
            statusArray[0] = "Pending";
            statusArray[1] = "Rejected";
            /*
             * Good
             * one
             */
            //logger.debug("Pulling Non Paged Data");


            HashMap filter = new HashMap();
            java.util.Date f = df.parse("01/01/2012 00:00:00");
            java.util.Date t = df.parse("12/31/2012 00:00:00");


            logger.info(">>>> filter on correct from and to date semantics (fails) >>>>");
            filter.put("Process Instance.Task Information.Assigned Date From", f);
            filter.put("Process Instance.Task Information.Assigned Date To", t);


            tcResultSet assignedTasks = null;

            assignedTasks = provOps.getAssignedOpenProvisioningTasks(1L, filter, statusArray);

            int rowCount = assignedTasks.getRowCount();
            for (int i = 0; i < rowCount; i++) {
                assignedTasks.goToRow(i);
                Date xdate2 = assignedTasks.getDate("Process Instance.Task Information.Assigned Date");
                int type = assignedTasks.getColumnType("Process Instance.Task Information.Assigned Date");
                logger.debug("Date:" + df.format(xdate2));
            }

            logger.info(">>>> filter on incorrect from and to semantics (works) >>>>");
            filter.put("Process Instance.Task Information.Assigned Date From", t);
            filter.put("Process Instance.Task Information.Assigned Date To", f);
            assignedTasks = provOps.getAssignedOpenProvisioningTasks(1L, filter, statusArray);
            rowCount = assignedTasks.getRowCount();
            for (int i = 0; i < rowCount; i++) {
                assignedTasks.goToRow(i);
                Date xdate2 = assignedTasks.getDate("Process Instance.Task Information.Assigned Date");
                int type = assignedTasks.getColumnType("Process Instance.Task Information.Assigned Date");
                logger.debug("Date:" + df.format(xdate2));
            }

            logger.info(">>>> FILTER ON TASKNAME >>>>");
            filter.clear();
            filter.put("Process Definition.Tasks.Task Name", "Grant Resource 1");
            assignedTasks = provOps.getAssignedOpenProvisioningTasks(1L, filter, statusArray);
            printResultSet(assignedTasks, filter);

            logger.info(">>>> FILTER ON INVALID TASKNAME EXPECT NO RESULTS >>>>");
            filter.clear();
            filter.put("Process Definition.Tasks.Task Name", "Grant Resource 2");
            assignedTasks = provOps.getAssignedOpenProvisioningTasks(1L, filter, statusArray);
            printResultSet(assignedTasks, filter);
            
            logger.info(">>>>  get all xelsysadm pending >>>>");
            filter.clear();
            assignedTasks = provOps.getAssignedOpenProvisioningTasks(1L, filter, statusArray);
            int allcnt = assignedTasks.getRowCount();
            
            logger.info(">>>> FILTER ON Assigned User Key >>>>");
            filter.clear();
            filter.put("Process Instance.Task Information.Assigned To User Key", "1");
            assignedTasks = provOps.getAssignedOpenProvisioningTasks(1L, filter, statusArray);
            printResultSet(assignedTasks);
            
            int keycnt = assignedTasks.getRowCount();
            
            logger.debug("ALL:" + allcnt);
            logger.debug("USR:" + keycnt);
            
            logger.info(">>>> FILTER ON Assigned User ID >>>>");
            filter.clear();
            filter.put("Process Instance.Task Information.Assigned To", "xelsysadm");
            assignedTasks = provOps.getAssignedOpenProvisioningTasks(1L, filter, statusArray);
            printResultSet(assignedTasks, filter);
            
            keycnt = assignedTasks.getRowCount();
            logger.debug("ALL:" + allcnt);
            logger.debug("USR:" + keycnt);

            
        } catch (Exception e) {
            logger.error("APIEDRROR", e);
        }
        //logger.debug("Pulling Paged Data");

        //assignedTasks = null;
        //String[] order1 = new String[]{"Requests.Key"};
        //assignedTasks = provOps.getAssignedOpenProvisioningTasksPaged(1L, filter, statusArray, 1, 10, order1, true, true);
        //printResultSet(assignedTasks);
        logger.debug("Done");
    }

    public void provpaged() {

        logger.info("*********** PAGING API ***********");
        try {

            String[] statusArray = new String[2];
            statusArray[0] = "Pending";
            statusArray[1] = "Rejected";
            /*
             * Good
             * one
             */
            //logger.debug("Pulling Non Paged Data");


            HashMap filter = new HashMap();
            HashMap emptyFilter = new HashMap();
            //filter.put("Process Definition.Tasks.Task Name","Grant Resource 2");
            java.util.Date f = df.parse("01/01/2012 00:00:00");
            java.util.Date t = df.parse("12/31/2012 00:00:00");


            logger.info(">>>> filter on correct from and to date semantics (fails) >>>>");
            filter.put("Process Instance.Task Information.Assigned Date From", f);
            filter.put("Process Instance.Task Information.Assigned Date To", t);


            tcResultSet assignedTasks = null;
            String[] orderBy = new String[]{"Process Definition.Tasks.Task Name"};
            assignedTasks = provOps.getAssignedOpenProvisioningTasksPaged(1L, filter, statusArray, 1, 10, orderBy, true, true);
            //printResultSet(assignedTasks,filter);

            int rowCount = assignedTasks.getRowCount();
            for (int i = 0; i < rowCount; i++) {
                assignedTasks.goToRow(i);
                Date xdate2 = assignedTasks.getDate("Process Instance.Task Information.Assigned Date");
                int type = assignedTasks.getColumnType("Process Instance.Task Information.Assigned Date");
                logger.debug("Date:" + df.format(xdate2));
            }

            logger.info(">>>> filter on incorrect from and to semantics (works) >>>>");
            filter.put("Process Instance.Task Information.Assigned Date From", t);
            filter.put("Process Instance.Task Information.Assigned Date To", f);
            assignedTasks = provOps.getAssignedOpenProvisioningTasksPaged(1L, filter, statusArray, 1, 10, orderBy, true, true);
            rowCount = assignedTasks.getRowCount();
            for (int i = 0; i < rowCount; i++) {
                assignedTasks.goToRow(i);
                Date xdate2 = assignedTasks.getDate("Process Instance.Task Information.Assigned Date");
                int type = assignedTasks.getColumnType("Process Instance.Task Information.Assigned Date");
                logger.debug("Date:" + df.format(xdate2));
            }

            logger.info(">>>> FILTER ON TASKNAME >>>>");
            filter.clear();
            filter.put("Process Definition.Tasks.Task Name", "Grant Resource 1");
            assignedTasks = provOps.getAssignedOpenProvisioningTasksPaged(1L, filter, statusArray, 1, 10, orderBy, true, true);
            printResultSet(assignedTasks, filter);

            logger.info(">>>> FILTER ON INVALID TASKNAME EXPECT NO RESULTS >>>>");
            filter.clear();
            filter.put("Process Definition.Tasks.Task Name", "Grant Resource 2");
            assignedTasks = provOps.getAssignedOpenProvisioningTasksPaged(1L, filter, statusArray, 1, 10, orderBy, true, true);
            printResultSet(assignedTasks, filter);
            
            logger.info(">>>>  get all xelsysadm pending >>>>");
            filter.clear();
            assignedTasks = provOps.getAssignedOpenProvisioningTasksPaged(1L, filter, statusArray, 1, 20, orderBy, true, true);
            int allcnt = assignedTasks.getRowCount();
            
            logger.info(">>>> FILTER ON Assigned User Key >>>>");
            filter.clear();
            filter.put("Process Instance.Task Information.Assigned To User Key", "1");
            assignedTasks = provOps.getAssignedOpenProvisioningTasksPaged(1L, filter, statusArray, 1, 20, orderBy, true, true);
            printResultSet(assignedTasks);
            
            int keycnt = assignedTasks.getRowCount();
            
            logger.debug("ALL:" + allcnt);
            logger.debug("USR:" + keycnt);
            
            logger.info(">>>> FILTER ON Assigned User ID >>>>");
            filter.clear();
            filter.put("Process Instance.Task Information.Assigned To", "1");
            assignedTasks = provOps.getAssignedOpenProvisioningTasksPaged(1L, filter, statusArray, 1, 20, orderBy, true, true);
            printResultSet(assignedTasks, filter);
            
            keycnt = assignedTasks.getRowCount();
            logger.debug("ALL:" + allcnt);
            logger.debug("USR:" + keycnt);

            
        } catch (Exception e) {
            logger.error("APIEDRROR", e);
        }
        //logger.debug("Pulling Paged Data");

        //assignedTasks = null;
        //String[] order1 = new String[]{"Requests.Key"};
        //assignedTasks = provOps.getAssignedOpenProvisioningTasksPaged(1L, filter, statusArray, 1, 10, order1, true, true);
        //printResultSet(assignedTasks);
        logger.debug("Done");
    }

    public void provsgpaged() {

        logger.info("*********** SUBGROUP PAGING API ***********");
        try {

            String[] orderBy = new String[]{"Process Definition.Tasks.Task Name"};

            String[] statusArray = new String[2];
            statusArray[0] = "Pending";
            statusArray[1] = "Rejected";
            /*
             * Good
             * one
             */
            //logger.debug("Pulling Non Paged Data");



            HashMap filter = new HashMap();
            HashMap emptyFilter = new HashMap();
            //filter.put("Process Definition.Tasks.Task Name","Grant Resource 2");
            java.util.Date f = df.parse("01/01/2012 00:00:00");
            java.util.Date t = df.parse("12/31/2012 00:00:00");


            logger.info(">>>> filter on correct from and to date semantics (fails) >>>>");
            filter.put("Process Instance.Task Information.Assigned Date From", f);
            filter.put("Process Instance.Task Information.Assigned Date To", t);


            tcResultSet assignedTasks = null;

            assignedTasks = provOps.getOpenProvisioningTasksAssignedToSubgroupsPaged(3023L, filter, statusArray, true, 1, 10, orderBy, true, true);
            //printResultSet(assignedTasks,filter);

            int rowCount = assignedTasks.getRowCount();
            for (int i = 0; i < rowCount; i++) {
                assignedTasks.goToRow(i);
                Date xdate2 = assignedTasks.getDate("Process Instance.Task Information.Assigned Date");
                int type = assignedTasks.getColumnType("Process Instance.Task Information.Assigned Date");
                logger.debug("Date:" + df.format(xdate2));
            }

            logger.info(">>>> filter on incorrect from and to semantics (works) >>>>");
            filter.put("Process Instance.Task Information.Assigned Date From", t);
            filter.put("Process Instance.Task Information.Assigned Date To", f);
            assignedTasks = provOps.getOpenProvisioningTasksAssignedToSubgroupsPaged(3023L, filter, statusArray, true, 1, 10, orderBy, true, true);
            rowCount = assignedTasks.getRowCount();
            for (int i = 0; i < rowCount; i++) {
                assignedTasks.goToRow(i);
                Date xdate2 = assignedTasks.getDate("Process Instance.Task Information.Assigned Date");
                int type = assignedTasks.getColumnType("Process Instance.Task Information.Assigned Date");
                logger.debug("Date:" + df.format(xdate2));
            }

            logger.info(">>>> FILTER ON TASKNAME >>>>");
            filter.clear();
            filter.put("Process Definition.Tasks.Task Name", "Grant Resource 1");
            assignedTasks = provOps.getOpenProvisioningTasksAssignedToSubgroupsPaged(3023L, filter, statusArray, true, 1, 10, orderBy, true, true);
            printResultSet(assignedTasks, filter);

            logger.info(">>>> FILTER ON INVALID TASKNAME EXPECT NO RESULTS >>>>");
            filter.clear();
            filter.put("Process Definition.Tasks.Task Name", "Grant Resource 2");
            assignedTasks = provOps.getAssignedOpenProvisioningTasks(1L, filter, statusArray);
            printResultSet(assignedTasks, filter);

            logger.info(">>> RUN EMPTY SUBGROUP >>>>");

            assignedTasks = provOps.getOpenProvisioningTasksAssignedToSubgroupsPaged(3023L, new HashMap(), new String[]{}, true, 1, 10, orderBy, true, true);
            printResultSet(assignedTasks);
            
            int allcnt = assignedTasks.getRowCount();
            
            logger.info(">>>> FILTER ON Assigned User Key >>>>");
            filter.clear();
            filter.put("Process Instance.Task Information.Assigned To User Key", "3023");
            assignedTasks = provOps.getOpenProvisioningTasksAssignedToSubgroupsPaged(1L, new HashMap(), new String[]{}, true, 1, 10, orderBy, true, true);
            printResultSet(assignedTasks);
            
            int keycnt = assignedTasks.getRowCount();
            
            logger.debug("ALL:" + allcnt);
            logger.debug("USR:" + keycnt);
            
            logger.info(">>>> FILTER ON Assigned User ID >>>>");
            filter.clear();
            filter.put("Process Instance.Task Information.Assigned To", "FFORESTER");
            assignedTasks = provOps.getOpenProvisioningTasksAssignedToSubgroupsPaged(1L, new HashMap(), new String[]{}, true, 1, 10, orderBy, true, true);
            printResultSet(assignedTasks, filter);
            
            keycnt = assignedTasks.getRowCount();
            logger.debug("ALL:" + allcnt);
            logger.debug("USR:" + keycnt);
        } catch (Exception e) {
            logger.error("APIEDRROR", e);
        }
        logger.debug("Done");
    }

    public void approvals() {
        try {
            
            /*
             * Requests.Creation Date
             * Requests.Update Date
             * Requests.Comment.Comment
             */
            HashMap filter = new HashMap();
            
            String[] orderBy = new String[]{"OTI.REQ_KEY"};
            
            tcResultSet rs = null;
            
            rs = requestOps.getPendingApprovalTasksAssignedToUserPaged(1L, filter, 1, 10, orderBy, true, true);
            printResultSet(rs);
            int allcnt = rs.getRowCount();
            
            logger.info(">>>> FILTER ON Assigned User Key >>>>");
            filter.clear();
            filter.put("Process Instance.Task Information.Assigned To User Key", "1");
            rs = requestOps.getPendingApprovalTasksAssignedToUserPaged(1L, filter, 1, 10, orderBy, true, true);
            int filtcnt = rs.getRowCount();
            
            logger.debug("ALL:" + allcnt);
            logger.debug("FIL:" + filtcnt);
            
            logger.info(">>>> FILTER ON Assigned User ID >>>>");
            filter.clear();
            filter.put("Process Instance.Task Information.Assigned To", "1");
            rs = requestOps.getPendingApprovalTasksAssignedToUserPaged(1L, filter, 1, 10, orderBy, true, true);
            filtcnt = rs.getRowCount();
            
            logger.debug("ALL:" + allcnt);
            logger.debug("FIL:" + filtcnt);
            
            /*
            logger.info(">>>> FILTER ON TASKNAME >>>>");
            filter.put("Process Definition.Tasks.Task Name", "Approve");
            rs = requestOps.getPendingApprovalTasksAssignedToUserPaged(1L, filter, 1, 10, orderBy, true, true);
            printResultSet(rs,filter);
            
            logger.info(">>>> FILTER ON INVALID TASKNAME EXPECT NO RESULTS >>>>");
            filter.put("Process Definition.Tasks.Task Name", "Approve1");
            rs = requestOps.getPendingApprovalTasksAssignedToUserPaged(1L, filter, 1, 10, orderBy, true, true);
            printResultSet(rs,filter);
             */
            /*
            logger.info(">>>> FILTER ON Requests.Creation Date >>>>");
            java.util.Date f = df.parse("09/12/2012 00:00:00");
            filter.clear();
            filter.put("Requests.Creation Date",f);
            rs = requestOps.getPendingApprovalTasksAssignedToUserPaged(1L, filter, 1, 10, orderBy, true, true);
            printResultSet(rs,filter);
            */
            
            /*
            logger.info(">>>> FILTER ON Requests.Update Date >>>>");
            filter.clear();
            filter.put("Requests.Update Date",f);
            rs = requestOps.getPendingApprovalTasksAssignedToUserPaged(1L, filter, 1, 10, orderBy, true, true);
            printResultSet(rs,filter);
           
            logger.info(">>>> FILTER ON Requests.Comments.Comments >>>>");
            filter.clear();
            filter.put("Requests.Comments.Comments","My Comment");
            rs = requestOps.getPendingApprovalTasksAssignedToUserPaged(1L, filter, 1, 10, orderBy, true, true);
            printResultSet(rs,filter);
            */
            
            
            
        } catch (Exception e) {
            logger.error("APIERROR", e);
        }
    }
}